<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();
if (isset($_GET['editcat'])) {
	if (isset($_POST['name'])) {
		if (empty($_POST['name'])) {
		Aurus::getInstance()->addContent( Adminarea::getInstance()->adminInfo(__('Назва категорії не повинна бути пуста')));
		} else {
			Mysql::getInstance()->update('categories',array('parent' => $_POST['parent'],'cat_name' => Mysql::getInstance()->escape($_POST['name'])),'catid=\''.$_GET['editcat'].'\'');
			Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Інформацію оновлено')));
			Cache::clear('n_categories');
		}
	} else {
		$catNfo = Factory::get('Module_news')->getArrayOfCategories();
		$thiscat = Mysql::getInstance()->select('categories','*','catid=\''.$_GET['editcat'].'\'');
		$thiscat = $thiscat[0];

		$cselect = '<select name="parent"><option value="0"> ----- </option>';
		foreach ($catNfo as $id => $name){
			if ($id == $thiscat['parent']) {
				$cselect .= '<option selected="selected" value="'.$id.'">'.$name.'</option>';
			} elseif ($id == $thiscat['catid']) {
				$cselect .= '<option disabled="disabled" value="'.$id.'">'.$name.'</option>';
			} else {
				$cselect .= '<option value="'.$id.'">'.$name.'</option>';
			}
		}
		$cselect .= '</select>';
		
		 Aurus::getInstance()->addContent(Aurus::getInstance()->form_start().Html::table(
		Html::tr(
		Html::td(__('Назва категорії')).
		Html::td(Html::tag('','input',array('name' => 'name','value' => $thiscat['cat_name'])))
		).Html::tr(
		Html::td(__('Батьківська категорія')).
		Html::td($cselect)
		).Html::tr(
		Html::td(Html::tag(Html::tag('','input',array('type' => 'submit','value' => __('Оновити'))),'center'),'','left','100%','top',2)
		)
		).'</form>');
	}
} elseif (isset($_GET['delete'])){
	Mysql::getInstance()->delete('categories','catid='.$_GET['delete']);
	Mysql::getInstance()->update('categories',array('parent' => '0'),'catid='.$_GET['delete']);
	Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Категорію видалено')));
	Cache::clear('n_categories');
} elseif (isset($_GET['add']) && $_GET['add'] == 'yes'){
	if (isset($_POST['name'])) {
		if (empty($_POST['name'])) {
			Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Ім\'я категорії не може бути порожнє')));
		} else {
			Mysql::getInstance()->insert('categories',array('cat_name' => Mysql::getInstance()->escape($_POST['name']),
			'parent' => $_POST['parent']));
			Cache::clear('n_categories');
			Aurus::getInstance()->addContent( Adminarea::getInstance()->adminInfo(__('Категорію створено')));
		}
	} else {
		$cats = Factory::get('Module_news')->getArrayOfCategories();
		$c = '<select name="parent">';
		$c .= '<option value="0"> ----- </option>';
		foreach ($cats as $id => $cat){
			$c .= '<option value="'.$id.'">'.$cat.'</option>';
		}
		$c .= '</select>';
		Aurus::getInstance()->addContent( Aurus::getInstance()->form_start().Html::table(
		Html::tr(
		Html::td(__('Назва категорії')).
		Html::td(Html::tag('','input',array('value' => '','name' => 'name')))
		).Html::tr(
		Html::td(__('Батьківська категорія')).
		Html::td($c)
		).Html::tr(
		Html::td(Html::tag(Html::tag('','input',array('type' => 'submit','value' => __('Створити'))),'center'),'','left','100%','top',2)
		)
		).'</form>');
	}
} else {
	$cats = Factory::get('Module_news')->getArrayOfCategories();
	$table = Html::tr(
	Html::td('ID','','left','5%').
	Html::td(__('Назва категорії'),'','left','22%').
	Html::td('','','right','73%'),'chead');
	foreach ($cats as $id => $cat){
		$table .= Html::tr(
		Html::td($id,'confclass','left','5%').
		Html::td($cat,'confclass','left','22%').
		Html::td('<a href="'.LINK_ROOT.'adminarea/?do=news&amp;todo=categories&amp;editcat='.$id.'">'.__('Змінити').'</a> | <a href="'.LINK_ROOT.'adminarea/?do=news&amp;todo=categories&amp;delete='.$id.'" onclick="return confirm(\'' . __('Видалити категорію?') .'\')">'.__('Видалити').'</a>','confclass','right','73%')
		);
	}
	Aurus::getInstance()->addContent(Html::table($table));
}